<template>
  <div class="join-teacher">
    <el-card class="join-card" v-if="currentTeacher">
      <template #header>
        <div class="card-header">
          <h3>当前老师信息</h3>
        </div>
      </template>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="用户名">
          <el-tag type="info">{{ currentTeacher.username }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="姓名">
          <span class="teacher-name">{{ currentTeacher.realName }}</span>
        </el-descriptions-item>
      </el-descriptions>
      <div class="tips-text">
        您已加入老师的班级，如需更换请联系管理员。
      </div>
    </el-card>

    <el-card class="join-card" v-else>
      <template #header>
        <div class="card-header">
          <h3>加入老师</h3>
        </div>
      </template>
      <el-form :model="form" :rules="rules" ref="joinForm" label-width="120px">
        <el-form-item label="邀请码" prop="teacherKey">
          <el-input v-model="form.teacherKey" placeholder="请输入老师提供的邀请码"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入您的真实姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleJoin" :loading="loading">加入</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'JoinTeacherPage'
}
</script>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { joinTeacher, getMyTeacher } from '@/api/student'
import { useRouter } from 'vue-router'

const router = useRouter()
const joinForm = ref(null)
const loading = ref(false)
const currentTeacher = ref(null)

const form = reactive({
  teacherKey: '',
  realName: ''
})

const rules = {
  teacherKey: [
    { required: true, message: '请输入邀请码', trigger: 'blur' },
    { min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur' }
  ],
  realName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ]
}

// 获取当前老师信息
const fetchTeacherInfo = async () => {
  try {
    const response = await getMyTeacher()
    console.log('Teacher Info Response:', response)
    if (response.code === 200 && response.data) {
      currentTeacher.value = response.data
    }
  } catch (error) {
    console.error('获取老师信息失败:', error)
    // 如果是400错误，说明未加入老师，这是正常的
    if (error.response?.status !== 400) {
      ElMessage.error(error.response?.data?.message || '获取老师信息失败')
    }
  }
}

const handleJoin = async () => {
  if (!joinForm.value) return
  try {
    await joinForm.value.validate()
    loading.value = true
    const response = await joinTeacher(form)
    if (response.code === 200) {
      ElMessage.success('成功加入老师班级')
      // 重新获取老师信息
      await fetchTeacherInfo()
      // 加入成功后跳转到个人信息页面
      router.push('/student/profile')
    } else {
      ElMessage.error(response.message || '加入失败')
    }
  } catch (error) {
    console.error('加入失败:', error)
    ElMessage.error(error.response?.data?.message || '加入失败')
  } finally {
    loading.value = false
  }
}

// 页面加载时获取老师信息
onMounted(() => {
  fetchTeacherInfo()
})
</script>

<style scoped>
.join-teacher {
  max-width: 800px;
  margin: 0 auto;
}

.join-card {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-descriptions {
  margin: 20px 0;
}

.teacher-name {
  font-size: 16px;
  color: #606266;
  margin-left: 8px;
}

.tips-text {
  margin-top: 20px;
  padding: 12px;
  background-color: #f4f4f5;
  border-radius: 4px;
  color: #909399;
  font-size: 14px;
  line-height: 1.4;
  text-align: center;
}
</style> 